<template>
  <div class="containers">
    <div class="container">
      <div class="handle-box">
        <el-button type="primary" icon="el-icon-plus" class="handle-add mr10" @click="addregion" size="mini">
          添加
        </el-button>
        <el-select v-model="code" @change="getDataPage" placeholder="请选择类型" size="mini" class="mr10">
          <el-option v-for="(item, index) in navlist" :key="item.ID" :label="item.name" :value="item.ID">
          </el-option>
        </el-select>
        <el-select v-model="class_first_id" @change="setfirst" placeholder="请选择一级分类" size="mini" class="mr10">
          <el-option v-for="(item, index) in firstlist" :key="item.ID" :label="item.name" :value="item.ID"></el-option>
        </el-select>
        <el-select v-model="class_second_id" placeholder="请选择二级分类" size="mini" class="mr10" @change="setsecond">
          <el-option v-for="(item, index) in secondlist" :key="item.ID" :label="item.name" :value="item.ID"></el-option>
        </el-select>
        <el-select v-model="class_third_id" placeholder="请选择三级分类" size="mini" class="mr10" @change="getDataPage">
          <el-option v-for="(item, index) in thirdlist" :key="item.ID" :label="item.name" :value="item.ID"></el-option>
        </el-select>
        <el-input style="width: 180px;" v-model="keyword" placeholder="关键词" class="handle-input mr10" size="mini"></el-input>
        <el-button type="primary" icon="el-icon-search" @click="getDataPage" class="mr10" size="mini">搜索</el-button>
      </div>
      <el-table :data="tableData" class="table" ref="multipleTable" header-cell-class-name="table-header" size="mini" border
        @selection-change="handleSelectionChange" v-loading="loading" :height="$tableHight">

        <el-table-column type="index" width="55" label="序号" align="center"></el-table-column>
        <el-table-column prop="name" label="商品名称" show-overflow-tooltip></el-table-column>
        <el-table-column prop="title" label="商品简介" show-overflow-tooltip></el-table-column>
        <el-table-column prop="price" label="商品价格"></el-table-column>
        <el-table-column prop="stock" label="商品库存"></el-table-column>
<!--        <el-table-column prop="discount" label="会员让利（%）"></el-table-column>-->
<!--        <el-table-column prop="platform_discount" label="平台让利（%）"></el-table-column>-->
        <el-table-column label="展示图片" align="center" width="100">
          <template slot-scope="scope" v-if="scope.row.img">
            <el-image class="table-td-thumb" :src="scope.row.img" :preview-src-list="[scope.row.img]"></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="sort" label="排序">
          <template slot-scope="scope">
            <el-input size="mini" v-model="scope.row.sort" @blur="shortcutUpdateGoods(scope.row, 'sort')"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="首页展示" align="center">
          <template slot-scope="scope">
            <el-tag size="mini" :type="scope.row.is_index ? 'success' : 'danger'"
              @click="shortcutUpdateGoods(scope.row, 'is_index')">{{
                scope.row.is_index ? '是' : '否' }}</el-tag>
          </template>
        </el-table-column>
        <!-- <el-table-column label="热卖展示" align="center">
          <template slot-scope="scope">
            <el-tag :type="scope.row.is_hot ? 'success' : 'danger'" @click="shortcutUpdateGoods(scope.row, 'is_hot')">{{
              scope.row.is_hot ? '是' : '否' }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="推荐展示" align="center">
          <template slot-scope="scope">
            <el-tag :type="scope.row.is_extension ? 'success' : 'danger'"
              @click="shortcutUpdateGoods(scope.row, 'is_extension')">{{
                scope.row.is_extension ? '是' : '否' }}</el-tag>
          </template>
        </el-table-column> -->
        <el-table-column label="商品上架" align="center">
          <template slot-scope="scope">
            <el-tag size="mini" :type="scope.row.state == 0 ? 'danger' : 'success'"
              @click="shortcutUpdateGoods(scope.row, 'state')">{{
                scope.row.state == 0 ? '下架' : '上架' }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" class="red"
              @click="handleDelete(scope.$index, scope.row)">删除</el-button>
            <el-button size="mini" type="text" @click="copy('/pages/commodity/details?id=' + scope.row.ID)">复制链接</el-button>

          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">

        <el-pagination @size-change="handleSizeChange" @current-change="handlePageChange"
          :current-page="query.pageIndex" :page-sizes="[20, 30, 40, 50]" :page-size="query.pageSize"
          layout="total, sizes, prev, pager, next, jumper" :total="pageTotal">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'basetable',
  data() {
    return {
      query: {
        pageIndex: 1,
        pageSize: 20
      },
      upimg: {
        token: '',
        key: new Date().valueOf()
      },
      code: 0,
      upload: '',
      editVisible: false,
      tableData: [],
      pageTotal: 0,
      imageUrl: '',
      videoUrl: '',
      type: 0,
      navlist: [],
      keyword: '',
      loading: false,
      class_first_id: '',
      class_second_id: '',
      class_third_id: '',
      firstlist: [],
      secondlist: [],
      thirdlist: [],
    };
  },
  created() {
    this.getNAV()
    try {
      if (this.$route.query.page) {
        this.query.pageIndex = this.$route.query.page
      }
    } catch {

    }
  },
  methods: {
    copy(row) {
      // 创建一个input框
      const input = document.createElement('input')
      // 将指定的DOM节点添加到body的末尾
      document.body.appendChild(input)
      // 设置input框的value值为复制的值
      const address = row
      input.setAttribute('value', address)
      // 选取文本域中的内容
      input.select()
      // copy的意思是拷贝当前选中内容到剪贴板
      // document.execCommand（）方法操纵可编辑内容区域的元素
      // 返回值为一个Boolean，如果是 false 则表示操作不被支持或未被启用
      if (document.execCommand('copy')) {
        document.execCommand('copy')
      }
      // 删除这个节点
      document.body.removeChild(input)
      // 弹窗：复制成功
      this.$message.success('复制成功');
      //
    },
    //修改size值
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.query.pageSize = val
      this.getData();
    },
    // 修改商品信息
    shortcutUpdateGoods(e, type) {
      if (type == 'is_index') {
        e.is_index = e.is_index ? 0 : 1
      }
      if (type == 'is_hot') {
        e.is_hot = e.is_hot ? 0 : 1
      }
      if (type == 'is_extension') {
        e.is_extension = e.is_extension ? 0 : 1
      }
      if (type == 'state') {
        e.state = e.state ? 0 : 1
      }
      this.request('admin/shortcutUpdateGoods/', 'post', { id: e.ID, is_index: e.is_index, is_hot: e.is_hot, is_extension: e.is_extension, sort: Number(e.sort), state: e.state }).then(res => {
        this.getData()
        if (res.errno == 0) {
          this.$message.success(res.errmsg);
        } else {
          this.$message.error(res.errmsg);
        }
      })
    },

    //商品分区修改
    getoption(e) {
      this.code = e
    },
    getDataPage() {
      this.query.pageIndex = 1
      this.getData();

    },
    //获取商品分区
    getNAV() {
      this.request('admin/partition/', 'get', { page: this.query.pageIndex, size: this.query.pageSize }).then(res => {
        this.navlist = [{ ID: 0, name: '全部' }, ...res.data.data]
        // this.code = res.data.data[0].ID

        this.getData()
      })
      this.request('admin/firstClass/', 'get', { page: 1, size: 100, keyword: '' }).then(res => {
        this.firstlist = res.data.data
      })
    },
    //获取2级分类
    setfirst() {
      this.class_second_id = ''
      this.request('admin/secondClass/', 'get', { class_first_id: this.class_first_id, page: 1, size: 100, keyword: '' }).then(res => {
        this.secondlist = res.data.data
        this.getData()
      })
    },
    //获取3级分类
    setsecond() {
      this.class_third_id = ''
      this.request('admin/thirdClass/', 'get', { class_second_id: this.class_second_id, page: 1, size: 100, keyword: '' }).then(res => {
        this.thirdlist = res.data.data
        this.getData()
      })
    },
    //获取数据
    getData() {
      this.loading = true
      this.request('admin/add_goods/', 'get', {
        page: this.query.pageIndex, size: this.query.pageSize, keyword: this.keyword, partition: this.code, class_first_id: this.class_first_id,
        class_second_id: this.class_second_id,
        class_third_id: this.class_third_id
      }).then(res => {
        // console.log(res)
        this.tableData = res.data.data
        this.pageTotal = res.data.total
      })
      this.loading = false
    },
    // 编辑
    handleEdit(e) {
      // console.log(e.content)
      this.$router.push({ path: '/commodity/addgoods', query: { id: e.ID, page: this.query.pageIndex } });
    },
    cancel() {
      this.getData();
    },
    // 修改排序
    operations(e, code) {
      // console.log(e)
      e.sort = Number(e.sort)
      // console.log(e)
      this.request('admin/add_news/', 'put', e).then(res => {
        if (res.errno == 0) {
          this.$message.success(res.errmsg);
        } else {
          this.$message.error(res.errmsg);
        }
      });
      this.editVisible = false;
      this.getData();
    },
    // 删除操作
    handleDelete(index, row) {
      // 二次确认删除
      this.$confirm('确定要删除吗？', '提示', {
        type: 'warning'
      })
        .then(() => {
          this.request('admin/add_goods/', 'delete', { ID: row.ID }).then(res => {
            if (res.errno == 0) {
              this.$message.success(res.errmsg);
              this.tableData.splice(index, 1)
              this.getData()
            } else {
              this.$message.error(res.errmsg);
            }
          });
        })
    },
    // 多选操作
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 添加
    addregion() {
      this.$router.push({ path: '/commodity/addgoods' })
    },
    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, 'pageIndex', val);
      this.getData();
    }
  }
};
</script>

<style scoped>
.container {
  padding: 20px;
}

.el-upload--text {
  width: 200px !important;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;

  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  /* width: 100px; */
  height: 100px;
  line-height: 100px;
  text-align: center;
}

.avatar {
  width: 100%;
  height: 178px;
  display: block;
}

.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}

.table {
  width: 100%;
  font-size: 14px;
}

.red {
  color: #ff0000;
}

.mr10 {
  margin-right: 10px;
}

.table-td-thumb {
  display: block;
  margin: auto;
  width: 30px;
  height: 30px;
  border-radius: 2px;
  .el-image img {
    object-fit: cover;
  }
}

/* 设置分页按钮样式 */
.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
/* 使用 /deep/ 或 >>> 来穿透 scoped 样式 */
::v-deep .el-table th.el-table__cell {
  background-color: #f2f2f2;
  /* 设置表头颜色 */
  color: #333;
  /* 设置表头文字颜色 */
}
</style>
